//
//  HBSearchResultController.m
//  HeartBeat
//
//  Created by karong on 2023/8/24.
//

#import "HBSearchResultController.h"
#import "HBSearchResultCell.h"
#import "HBSearchRequest.h"
#import "HBSearchResultFooterView.h"
@interface HBSearchResultController () <HBSearchResultFooterViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) HBSearchRequest * request;
@property (strong, nonatomic) NSArray * hotSearchArray;

@end

@implementation HBSearchResultController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.bg_gray_f2f2f2;
    self.request = [HBSearchRequest new];
    self.view.hidden = YES;
    [self setEmptyStateTitle:WSLocalized(@"抱歉～！沒有搜索到相關內容\n可以看看其他推薦哦～")state:HBListViewEmptyStateEmpty];
}

- (void)searchWithKeyWord:(NSString *)keyWord {
    self.request.keyWord = keyWord;
    [self refreshData];
}

- (void)showNovelDetailWithNovel:(id)novel {
    [ViewManager pushNovelDetailVCWithNovel:novel inController:self];
}
#pragma mark RefreshResultProtocol
- (void)loadCompleteWithArray:(NSArray *)array {
    [super loadCompleteWithArray:array];
    [self.view endEditing:YES];
    self.view.hidden = NO;
}

- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    [super loadSuccessWithArray:array isRefresh:isRefresh];
    if ([self.delegate respondsToSelector:@selector(searchResultControllerSearchSuccess:)]) {
        [self.delegate searchResultControllerSearchSuccess:self.request.keyWord];
    }
    [self.listView reloadData];
}

- (void)loadFailedWithError:(NSError *)error {
    [super loadFailedWithError:error];
}

- (BOOL)disableAutoBeginRefresh {
    return NO;
}

#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MAX(self.dataArray.count, 1);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count > 0) {
        HBSearchResultCell * searchResultCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBSearchResultCell" forIndexPath:indexPath];
        [searchResultCell setDataWithObject:self.dataArray[indexPath.row] keyWord:self.request.keyWord];
        return searchResultCell;
    }
    else {
        UICollectionViewCell * searchResultEmptyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBSearchResultEmptyCell" forIndexPath:indexPath];
        return searchResultEmptyCell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth - 12 - 12;
    if (self.dataArray.count > 0) {
        size.height = 92;
    }
    else {
        size.height = 157;
    }
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        HBSearchResultFooterView * searchResultFooterView;
        if (!searchResultFooterView) {
            searchResultFooterView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HBSearchResultFooterView" forIndexPath:indexPath];
            searchResultFooterView.delegate = self;
            [searchResultFooterView setDataWithObject:self.hotSearchArray];
        }
        return searchResultFooterView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    if (self.dataArray.count == 0) {
        size.height = 20 + (70 + 12) * 5 + 48;
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataArray.count > 0) {
        HBNovelModel * novel = self.dataArray[indexPath.row];
        [self showNovelDetailWithNovel:novel];
    }
}

#pragma mark HBSearchFooterViewDelegate
- (void)searchResultFooterViewActionWithObjct:(HBNovelModel *)object {
    [self showNovelDetailWithNovel:object];
}

- (NSArray *)hotSearchArray {
    if ([self.delegate respondsToSelector:@selector(searchResultControllerHotSearchArray)]) {
        return [self.delegate searchResultControllerHotSearchArray];
    }
    else {
        return @[];
    }
}

@end
